package com.blog.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.dao.UserDao;
import com.blog.entity.User;
import com.blog.myUtils.DateTime;
import com.blog.myUtils.Security;
import com.blog.service.CertService;
import com.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;


@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private CertService certService;

    public User loginAction(User user, HttpSession session) {
        user.setLoginAt(DateTime.getTime());
        user = this.save(user);
        user.setPassword("");
        user.setPasswordRestToken("");
        session.setAttribute("user", user);
        session.setAttribute("isLogin", true);
        session.setAttribute("cert", certService.getCertsByUid(user.getId()));
        System.out.println(certService.getCertsByUid(user.getId()));
        return user;
    }

    /**
     * 用户登录
     *
     * @param email
     * @param password
     * @return
     */
    @Override
    public User login(String email, String password, HttpSession session) {
        User user = new User();
        String token = Security.md5Salt(password);
//        System.out.println("service session:" + session);
        user = user.selectOne(new EntityWrapper<User>().eq("email", email));
        if (user != null && user.getStatus() == User.STATUS_ACTIVE && user.getPassword().equals(token)) {
            user = loginAction(user, session);
            return user;
        } else {
            return null;
        }
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @Override
    public User register(User user, HttpSession session) {
        user.setPassword(Security.md5Salt(user.getPassword()));
        user.setStatus(User.STATUS_ACTIVE);
        user = this.loginAction(this.save(user), session);
        return user;
    }

    /**
     * @param user
     * @return
     */
    public User save(User user) {
        //TODO: 处理头像上传
        boolean flag = false;
        if (user.getId() != null && user.selectById(user.getId()) != null) {
            user.setCreatedAt(DateTime.getTime());
            user.setUpdatedAt(user.getCreatedAt());

            flag = user.updateById();
        } else {
            user.setUpdatedAt(DateTime.getTime());
            flag = user.insert();
        }
        return flag ? user : null;
    }


    /**
     * @param page
     * @param state
     * @return
     */
    @Override
    public Page<User> getUsers(Page<User> page, Integer state) {
        return new User().selectPage(page, "1=1");
    }


    /**
     * 修改个人资料
     *
     * @param user
     * @param session
     * @return
     */
    @Override
    public User updateInfo(User user, HttpSession session) {
        user = this.save(user);
        session.setAttribute("user", user);
        return user;
    }

    @Override
    public boolean forbidden(Integer id) {
        User user = new User();
        user = user.selectById(id);
        user.setStatus(User.STATUS_FROBIDDEN);
        if (save(user) != null) {
            return true;
        }
        return false;
    }

    @Override
    public User view(Integer id) {
        return new User().selectById(id);
    }

    @Override
    public boolean access(Integer id) {
        User user = new User();
        user = user.selectById(id);
        user.setStatus(User.STATUS_ACTIVE);
        if (save(user) != null) {
            return true;
        }
        return false;
    }


}
